﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using ALinq.BBS.Common.Config;
using System.Web.Configuration;

namespace ALinq.BBS.Common
{
    public class ForumSection : ConfigurationSection
    {
        const string GROUP_NAME = "ALinqBBS";

        private Configuration config;
        private static ForumSection instance;

        public static string ConnectionName
        {
            get { return "Default"; }
        }

        public new object this[string name]
        {
            get
            {
                return base[name];
            }
        }

        /// <summary>
        /// 论坛虚拟路径
        /// </summary>
        [ConfigurationProperty("virtualPath")]
        public string VirtualPath
        {
            get { return (string)base["virtualPath"]; }
            set { base["VirtualPath"] = value; }
        }

        /// <summary>
        /// 论坛名称
        /// </summary>
        [ConfigurationProperty("name")]
        public string ForumName
        {
            get { return (string)base["name"]; }
            set { base["name"] = value; }
        }

        /// <summary>
        /// 站点描述
        /// </summary>
        [ConfigurationProperty("description")]
        public string Description
        {
            get { return (string)base["description"]; }
            set { base["description"] = value; }
        }

        [ConfigurationProperty("register")]
        public RegisterElement Register
        {
            get { return (RegisterElement)base["register"]; }
        }


        [ConfigurationProperty("userHeader")]
        public UserPhotoElement UserHeader
        {
            get { return (UserPhotoElement)base["userHeader"]; }
        }

        [ConfigurationProperty("keyWords")]
        public string KeyWords
        {
            get { return (string)base["keyWords"]; }
            set { base["keyWords"] = value; }
        }

        /// <summary>
        /// 返回页面的扩展名。
        /// </summary>
        [ConfigurationProperty("ext", DefaultValue = ".aspx")]
        public string PageExtension
        {
            get { return (string)base["ext"]; }
            set { base["ext"] = value; }
        }

        [ConfigurationProperty("adminEmail", DefaultValue = "ansiboy@163.com")]
        public string AdminEmail
        {
            get { return (string)base["adminEmail"]; }
            set { base["adminEmail"] = value; }
        }

        /// <summary>
        /// 帖子列表每页所显示的行数。
        /// </summary>
        [ConfigurationProperty("pageCount", DefaultValue = 20)]
        public int PostListSize
        {
            get { return (int)base["pageCount"]; }
            set { base["pageCount"] = value; }
        }

        /// <summary>
        /// 回复每页显示的行数。
        /// </summary>
        [ConfigurationProperty("replyListSize", DefaultValue = 10)]
        public int ReplyListSize
        {
            get { return (int)base["replyListSize"]; }
            set { base["replyListSize"] = value; }
        }

        /// <summary>
        /// 在线用户的刷新时间，单位分钟。
        /// </summary>
        [ConfigurationProperty("userInterval", DefaultValue = 10)]
        public int UserInterval
        {
            get { return (int)base["userInterval"]; }
            set { base["userInterval"] = value; }
        }

        [ConfigurationProperty("adminQQ")]
        public string AdminQQ
        {
            get { return (string)base["adminQQ"]; }
            set { base["adminQQ"] = value; }
        }

        [ConfigurationProperty("themeName")]
        public string ThemeName
        {
            get { return (string)base["themeName"]; }
            set { base["themeName"] = value; }
        }

        /// <summary>
        /// 返回JavaScript脚本的虚拟路径。
        /// </summary>
        public string ScriptPath
        {
            get { return VirtualPath + "Script/"; }
        }

        [ConfigurationProperty("header")]
        public UserPhotoElement Header
        {
            get { return UserHeader; }
        }

        [ConfigurationProperty("webSite")]
        public string WebSite
        {
            get
            {
                return (string)base["webSite"];
                //"http://" + HttpContext.Current.Request.ServerVariables["HTTP_HOST"]; 
            }
            set { base["webSite"] = value; }
        }

        /// <summary>
        /// 论坛系统的物理路径。
        /// </summary>
        [ConfigurationProperty("physicalPath")]
        public string PhysicalPath
        {
            get { return (string)base["physicalPath"]; }
            set { base["physicalPath"] = value; }
        }

        public string EmailTemplatesPath
        {
            get { return VirtualPath + "EmailTemplates/"; }
        }

        /// <summary>
        /// 插件文件夹的相对路径　
        /// </summary>
        public const string PlugRelativePath = "Plug/";

        /// <summary>
        /// 插件文件夹的虚拟路径　
        /// </summary>
        public string PlugPath
        {
            get { return VirtualPath + PlugRelativePath; }
        }

        [ConfigurationProperty("templatePath", DefaultValue = "Template/")]
        public string TemplatePath
        {
            get
            {
                return (string)base["templatePath"];
            }
        }

        public string ThemePath
        {
            get { return VirtualPath + TemplatePath + ThemeName + "/"; }
        }

        public string ThemePhysicalPath
        {
            get { return PhysicalPath + TemplatePath + ThemeName + "/"; }
        }

        public string DefaultPhoto
        {
            get
            {
                return VirtualPath + "UserFace/0.gif";
            }
        }

        public Configuration Config { get; private set; }

        public static ForumSection Instance
        {
            get
            {
                if (instance == null)
                    instance = CreateInstance();
                return instance;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        public static ForumSection CreateInstance()
        {
            var forumSection = WebConfigurationManager.GetSection(GROUP_NAME + "/forum") as ForumSection;
            if (forumSection == null)
            {
                var e = new Exception("Forum没有配置好。");
                throw e;
            }
            forumSection.Config = WebConfigurationManager.OpenWebConfiguration("~");
            return forumSection;
            //try
            //{
            //    string vPath = forumSection.VirtualPath;
            //    Configuration c = WebConfigurationManager.OpenWebConfiguration(vPath);
            //    return CreateInstance(c);
            //}
            //catch (Exception e)
            //{
            //    Logger.OutputException(e);
            //    throw;
            //}
        }

        /// <summary>
        /// 保存所作的更改。
        /// </summary>
        public void Save()
        {
            config.Save(ConfigurationSaveMode.Modified, false);
        }

        public static ForumSection CreateInstance(Configuration webConfig)
        {
            try
            {
                var forumSection = (ForumSection)webConfig.GetSection(GROUP_NAME + "/forum");
                forumSection.config = webConfig;
                return forumSection;
            }
            catch (Exception e)
            {
                Logger.OutputException(e);
                throw;
            }
        }

        public string KindEditorDotNetPath
        {
            get { return @"~/Scripts/kindeditor/asp.net"; }
        }
    }
}
